﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;


namespace Perpus
{
    public partial class KembaliBuku : Form
    {
        public KembaliBuku()
        {
            InitializeComponent();
        }

        NhibernateHelper help = new NhibernateHelper();

        //string koneksi = MappingDatabase.koneksi;

        //private ISessionFactory CreateSessionFactory()
        //{
        //    return Fluently.Configure()
        //        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(koneksi))
        //        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PeminjamanMap>())
        //        .BuildSessionFactory();
        //}


        private void KembaliBuku_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Peminjaman' table. You can move, or remove it, as needed.
            this.peminjamanTableAdapter.Fill(this.dotnetDataSet.Peminjaman);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            admin_MenuAwal menu = new admin_MenuAwal();
            menu.Show();
            this.Hide();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            textBox1.Text = dataGridView1[0, i].Value.ToString();
            textBox2.Text = dataGridView1[1, i].Value.ToString();
            textBox3.Text = dataGridView1[2, i].Value.ToString();
            textBox4.Text = dataGridView1[3, i].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            int idpinjam = Convert.ToInt32(textBox1.Text);
            int noreg = Convert.ToInt32(textBox4.Text);
            int idpeg = Convert.ToInt32(textBox3.Text);

            try
            {
                using (var session = factory.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var book = new Peminjaman
                        {
                            id_peminjaman = idpinjam,
                            nomor_registrasi = session.Get<Anggota>(noreg),
                            id_pegawai = session.Get<Pegawai>(idpeg),
                            tanggal_peminjaman = Convert.ToDateTime(textBox2.Text)
                          
                        };

                        session.Delete(book);
                        transaction.Commit();
                        MessageBox.Show("Buku Telah Dikembalikan!");
                        //dataGridView1.DataSource = dotnetDataSet.Buku;
                    }
                }

            }
            catch
            {
                MessageBox.Show("Gagal!");
            }

        }
    }
}
